package com.njym.java.security.app.rest;

import com.njym.java.security.app.biz.PhotoAlbumBiz;
import com.njym.java.security.app.entity.PhotoAlbum;
import com.njym.java.security.app.entity.PhotoAlbumVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("photoAlbum")
public class PhotoAlbumController extends BaseController<PhotoAlbumBiz,PhotoAlbum> {
    @RequestMapping(value = "/create",method = RequestMethod.GET)
    @ResponseBody
    public Object createAlbum(@RequestParam String albumName, @RequestParam String userId,
                                             @RequestParam int isShow, @RequestParam int theme){
        if (!userId.equals("undefined")) {
            PhotoAlbum album = new PhotoAlbum();
            album.setPhotoName(albumName);
            album.setIsShow(isShow);
            album.setTheme(theme);
            album.setDeleted(0);
            album.setAttr1(Integer.parseInt(userId));
            baseBiz.insertSelective(album);
            return new ObjectRestResponse<>();
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    //用户获取自己的相册
    /**
     *
     * @param userId 用户id
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @ResponseBody
    public Object lists(@RequestParam String userId){
        if (!userId.equals("undefined")) {
            List<PhotoAlbumVo> list = baseBiz.selectLists(Integer.parseInt(userId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<PhotoAlbumVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    //用户获取好友的的相册
    /**
     *
     * @param userId 好友的用户id
     * @return
     */
    @RequestMapping(value = "/getOpenAlbums",method = RequestMethod.GET)
    @ResponseBody
    public Object getOpenAlbums(@RequestParam String userId){
        if (!userId.equals("undefined")) {
            List<PhotoAlbumVo> list = baseBiz.getOpenAlbums(Integer.parseInt(userId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<PhotoAlbumVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/updateName",method = RequestMethod.GET)
    @ResponseBody
    public Object updateName(@RequestParam String photoName, @RequestParam String id, @RequestParam String userId){
        if (!userId.equals("undefined") && !id.equals("undefined")) {
            baseBiz.updateName(photoName, Integer.parseInt(id), Integer.parseInt(userId));
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    @RequestMapping(value = "/deleteAlbum",method = RequestMethod.GET)
    @ResponseBody
    public Object deleteAlbum(@RequestParam String id, @RequestParam String userId){
        if (!userId.equals("undefined") && !id.equals("undefined")) {
            baseBiz.deleteAlbum(Integer.parseInt(id), Integer.parseInt(userId));
            return new ObjectRestResponse<>().rel(true);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

}